package com.sims.action;

import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;
import com.sims.eo.SimsCourse;
import com.sims.service.ICourseService;
import com.sims.service.impl.CourseServiceImp;

public class CoursePageAction extends ActionSupport {
	private String courseId;
	private SimsCourse course = null;
	ICourseService courseService = new CourseServiceImp();

	// -----------分页获取数据---------------
	private int pageNo = 1;
	private int pageSize = 4;
	private int pageCount;
	private long rowCount;

	public String getCourseId() {
		return courseId;
	}

	public void setCourseId(String courseId) {
		this.courseId = courseId;
	}

	public SimsCourse getCourse() {
		return course;
	}

	public void setCourse(SimsCourse course) {
		this.course = course;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public long getRowCount() {
		return rowCount;
	}

	public void setRowCount(long rowCount) {
		this.rowCount = rowCount;
	}

	// 获取所有课程
	public String listCourse() throws Exception {
		System.out.println("-----listCoursePage");
		rowCount = courseService.getRowCount();
		// 获取总页数
		if (rowCount % pageSize == 0) {
			pageCount = (int) (rowCount / pageSize);
		} else {
			pageCount = (int) (rowCount / pageSize) + 1;
		}
		if (pageNo < 1) {
			pageNo = 1;
		} else if (pageNo > pageCount) {
			pageNo = pageCount;
		}
		// 分页获取记录
		List<SimsCourse> list = courseService
				.getAllSimsCourse(pageNo, pageSize);
		ActionContext ctx = ActionContext.getContext();
		ctx.put("CourseList", list);
		return "list";
	}

	// 删除账号
	public String deleteCourse() throws Exception {
		System.out.println("-----deleteCourse id=" + courseId);
		Long id = Long.parseLong(courseId);
		boolean bFlag = courseService.deletSimsCourseById(id);
		if (bFlag) {
			this.addActionMessage("删除数据成功！");
		} else {
			this.addActionMessage("删除数据失败！");
		}
		return listCourse();
	}

	// 修改课程
	public String editCourse() throws Exception {
		System.out.println("-----editCourse id=" + courseId);
		Long id = Long.parseLong(courseId);
		course = courseService.getSimsCourseById(id);
		if (course == null) {
			// 未获取到用户对象给提示
			this.addActionError("id=" + id + ",未获取到用户对象！");
		}
		return "edit";
	}

	// 编辑账号
	@InputConfig(resultName = "edit_input")
	public String updateCourse() throws Exception {
		System.out.println("-----updateCourse");
		boolean bFlag = courseService.updateSimsCourse(course);
		if (bFlag) {
			// 修改成功
			return "go_list";
		} else {
			this.addActionError("您填写的课程名称已存在！请重新填写！");
			return "edit";
		}
	}

	// 编辑课程Action校验方法
	public void validateUpdateCourse() throws Exception {
		System.out.println("----validateUpdateCourse");
		if (course == null) {
			// 未获取到课程对象给提示
			this.addActionError("未获取到课程对象！");
		} else {
			if (course.getCourseCode() == null
					|| course.getCourseCode().trim().length() <= 0) {
				this.addFieldError("courseCodeInfo", "请输入课程代码 ！");
			}
			if (course.getCourseName() == null
					|| course.getCourseName().trim().length() <= 0) {
				this.addFieldError("courseNameInfo", "请输入课程名称！");
			}
			if (course.getPeriod() == null) {
				this.addFieldError("periodInfo", "请填写课时！");
			}
			if (course.getCredit() == null) {
				this.addFieldError("creditInfo", "请填写学分 ！");
			}
		}
	}

	@InputConfig(resultName = "create_input")
	public String createCourse() throws Exception {
		System.out.println("----createCourse---");
		ICourseService courseService = new CourseServiceImp();
		Long id = courseService.createCourse(course);
		if (id > 0) {
			// 新建成功
			this.addActionError(course.getCourseName() + ",新建成功！");
			return listCourse();
		} else {
			// 新建失败
			this.addActionError(course.getCourseName() + "您填写的课程名称已存在！请重新填写！");
			return "create";
		}
	}

	// 注册Action校验方法
	public void validateCreateCourse() throws Exception {
		System.out.println("----validateCreateCourse---");
		if (course == null) {
			// 未获取到课程对象给提示
			this.addActionError("未获取到课程对象！");
		} else {
			if (course.getCourseCode() == null
					|| course.getCourseCode().trim().length() <= 0) {
				this.addFieldError("courseCodeInfo", "请输入课程代码 ！");
			}
			if (course.getCourseName() == null
					|| course.getCourseName().trim().length() <= 0) {
				this.addFieldError("courseNameInfo", "请输入课程名称！");
			}
			if (course.getPeriod() == null) {
				this.addFieldError("periodInfo", "请填写课时！");
			}
			if (course.getCredit() == null) {
				this.addFieldError("creditInfo", "请填写学分 ！");
			}
		}
	}
}
